package com.cx.utils;

import com.cx.property.LettuceProperties;
import com.cx.support.SpringContextHolder;
import io.lettuce.core.support.BoundedPoolConfig;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import java.util.List;

/**
 * Created on 2019-04-18 15:42.
 * @author 舒建辉
 */
public class SerializeAdapter {

    private static SpringContextHolder springContext;

    public static void init(SpringContextHolder springContextHolder){
        springContext = springContextHolder;
    }

    public static LettuceProperties lettuceProperties(){
        return springContext.getBean(LettuceProperties.class);
    }

    private static String serializeType = lettuceProperties().getSerializeType();

    public static BoundedPoolConfig boundedPoolConfig(){
        return springContext.getBean(BoundedPoolConfig.class);
    }

    public static GenericObjectPoolConfig poolConfig(){
        return springContext.getBean(GenericObjectPoolConfig.class);
    }


    /**
     * 序列化对象
     * @param object
     * @return
     */
    public static <T> String serialize(T object) {
        switch (serializeType){

            case CacheConsts.SERIALIZE_TYPE_KRYO : return KryoUtil.serialize(object);

            case CacheConsts.SERIALIZE_TYPE_PROTOSTUFF : return ProtoStuffUtil.serialize(object);

            case CacheConsts.SERIALIZE_TYPE_FASTJSON : return JsonHelper.serialize(object);

            default: return FstUtil.serialize(object);
        }
    }

    /**
     * 反序列化对象
     * @param serializedStr
     * @param clz
     * @return
     */
    public static <T> T deserialize(String serializedStr, Class<T> clz) {
        switch (serializeType){

            case CacheConsts.SERIALIZE_TYPE_KRYO : return KryoUtil.deserialize(serializedStr, clz);

            case CacheConsts.SERIALIZE_TYPE_PROTOSTUFF : return ProtoStuffUtil.deserialize(serializedStr, clz);

            case CacheConsts.SERIALIZE_TYPE_FASTJSON : return JsonHelper.deserialize(serializedStr, clz);

            default: return FstUtil.deserialize(serializedStr, clz);
        }
    }

    /**
     * 序列化List对象
     * @param list
     * @return
     */
    public static <T> String serializeList(List<T> list) {
        switch (serializeType){

            case CacheConsts.SERIALIZE_TYPE_KRYO : return KryoUtil.serializeList(list);

            case CacheConsts.SERIALIZE_TYPE_PROTOSTUFF : return ProtoStuffUtil.serializeList(list);

            case CacheConsts.SERIALIZE_TYPE_FASTJSON : return JsonHelper.serialize(list);

            default: return FstUtil.serializeList(list);
        }
    }

    /**
     * 反序列化List对象
     * @param serializedStr
     * @return
     */
    public static <T> List<T> deserializeList(String serializedStr, Class<T> clz) {
        switch (serializeType){

            case CacheConsts.SERIALIZE_TYPE_KRYO : return KryoUtil.deserializeList(serializedStr, clz);

            case CacheConsts.SERIALIZE_TYPE_PROTOSTUFF : return ProtoStuffUtil.deserializeList(serializedStr, clz);

            case CacheConsts.SERIALIZE_TYPE_FASTJSON : return JsonHelper.deserializeList(serializedStr, clz);

            default: return FstUtil.deserializeList(serializedStr, clz);
        }
    }
}